let rootPath = "./";
let content = files.read(rootPath+"config.txt");
function buildId() {
    return device.buildId;
}

function clickItemInCenter(item, time) {
    if (time == null) time = 50;
    if (item == null) return;
    let x = item.bounds().centerX();
    let y = item.bounds().centerY();
    press(x, y, time);
}

function openBtnItem(item) {
    let o = textContains(item).findOnce();
    if (o == null) {
        return -1;
    }
    clickItemInCenter(o);
    sleep(600);
    return 1;
}

// 获取当前时间的秒数  
function getCurrentSecond() {  
    let now = new Date();  
    return now.getSeconds();  
}  
  
// 采购订单  
function buyDTask(x,y) {  
    openBtnItem("最高采购价");
    sleep(300);   
    let inputs = className("android.widget.EditText").find();
    for (let i = 0; i < inputs.length; i++) {
        if(i == 0){
            inputs[i].setText(x);
        }else{
            inputs[i].setText(y);
        }
    }
    sleep(800);
    openBtnItem("采购 订货");
} 

//采购转货
function buyZTask(x,y) {  
    openBtnItem("最高采购价");
    sleep(500);
    //查询 采购 订货 的位置
    let o1 = textContains('采购 订货').findOnce();
    sleep(200);   
    openBtnItem("转货");
    sleep(200);
    let inputs = className("android.widget.EditText").find();
    for (let i = 0; i < inputs.length; i++) {
        if(i == 0){
            inputs[i].setText(x);
        }else{
            inputs[i].setText(y);
        }
        sleep(100);
    }
    sleep(500);
    clickItemInCenter(o1);
} 

// 销售订货
function sellDTask(x,y) {  
    openBtnItem("最低销售价");
    sleep(300);
    let inputs = className("android.widget.EditText").find();
    for (let i = 0; i < inputs.length; i++) {
        if(i == 0){
            inputs[i].setText(x);
        }else{
            inputs[i].setText(y);
        }
    }
    sleep(800);
    openBtnItem("销售 订货");
} 

// 销售转货 
function sellZTask(x,y) {  
    openBtnItem("最低销售价");
    sleep(500);
    let o = textContains('销售 订货').findOnce();
    sleep(200); 
    openBtnItem("转货");
    let inputs = className("android.widget.EditText").find();
    for (let i = 0; i < inputs.length; i++) {
        if(i == 0){
            inputs[i].setText(x);
        }else{
            inputs[i].setText(y);
        }
        sleep(100);
    }
    sleep(500);
    clickItemInCenter(o);
} 

// 撤销 采购
function cancelBuyTask() {  
    openBtnItem("一键撤单");
    sleep(300);
    openBtnItem("采购全撤单");
    sleep(300);
    openBtnItem("刷新");
} 

// 撤销 销售
function cancelSellTask() {  
    openBtnItem("一键撤单");
    sleep(300);
    openBtnItem("销售全撤单");
    sleep(300);
    openBtnItem("刷新");
} 

//0、初始状态 1、挂买入 2、买入成功，挂卖出 3、卖出成功回到初始，到达点位 第一手未卖出，撤销订单，挂第二手买入，
//4、第二手未买入成功撤销订单重新买入，买入成功，挂卖出；5、第二手未卖出，撤销订单，挂第三手买入
//6、第二手未买入成功撤销订单重新买入，第二手买入成功，挂卖出；7、第三手未卖出，撤销订单

let runup = 0 //标记买多买空
let runstutas = 0  
let runvalue = 0

function run() {
    let in1 = files.read(rootPath+"in1.txt");
    //计算每手的数量
    toastLog("双针快打,开始..."+in1);
    let arr = in1.split(',');
    while (true) {
        let currentSecond = getCurrentSecond(); 
        if (currentSecond > 5 && currentSecond < 8) {
            toastLog("双针快打,程序运行中"+currentSecond);
        }  

        if (currentSecond > 15 && currentSecond < 18) {
            toastLog("双针快打,程序运行中"+currentSecond);
        } 

        if (currentSecond > 25 && currentSecond < 28) {
            toastLog("双针快打,程序运行中"+currentSecond);
        }

        if (currentSecond > 35 && currentSecond < 38) {
            toastLog("双针快打,程序运行中"+currentSecond);
        } 

        if (currentSecond > 45 && currentSecond < 48) {
            toastLog("双针快打,程序运行中"+currentSecond);
        }
        //根据接口数据确定是否购买
        if (currentSecond > 57) {  
            toastLog("双针快打"); 
            let url2 = "https://anauto.millsecond.cn/api/agro/get_opportunity";
            r = http.get(url2, {
                headers: {
                    'Accept-Language': 'zh-cn,zh;q=0.5',
                    'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
                }
            });
            toastLog("双针探底1");
            let result2 = r.body.json();
            if(result2.code == 200){
                toastLog("双针探底2");
                const postData = {  mobile: content,buildId:buildId() };
                let url = "https://anauto.millsecond.cn/api/user/orderList";
                r = http.postJson(url, postData);
                let result = r.body.json();
                if(result.code == 200){
                    let numt = result.data.num;
                    if(runstutas == 0){
                        toastLog("双针探底3");
                        //判断是否还有挂单，还有挂单无法买入
                        if(parseInt(numt,10) == 0){
                            toastLog("双针探4"+numt);
                            // zc yl
                            if(parseInt(result2.data.close,10) >= parseInt(result2.data.zc,10)){
                                if(parseInt(result2.data.close,10) - parseInt(result2.data.zc,10) < 3){
                                    toastLog("双针探底,zc线太近");  
                                    continue;       
                                }
                            }
                            if(parseInt(result2.data.close,10) <= parseInt(result2.data.yl,10)){
                                if(parseInt(result2.data.yl,10) - parseInt(result2.data.close,10)  < 3){
                                    toastLog("双针探底,yl线太近"); 
                                    continue;       
                                }
                            }

                            if(result2.data.mode == 1){
                                toastLog("双针探底,买涨"); 
                                //在60线上方
                                if(parseInt(result2.data.close,10) > parseFloat(result2.data.ssprice)){
                                    //在黄线上方
                                    // if((parseInt(result2.data.close,10)) > parseFloat(result2.data.sprice)){
                                        //操作买多
                                        toastLog("双针探底,第一轮,买多;价格:"+result2.data.close+";数量:"+arr[0]); 

                                        buyDTask(parseInt(result2.data.close,10),parseInt(arr[0],10));
                                        runup = 1; 
                                        runstutas = 1; 
                                        runvalue = parseInt(result2.data.close,10); 
                                    // }else{
                                    //     toastLog("双针探底,买多,k值在60线下方");
                                    // }
                                }else{
                                    toastLog("双针探底,买多,k值在60线下方"); 
                                }
                            }
                            //买跌
                            if(result2.data.mode == 2){
                                //在60线上方
                                toastLog("双针探底,买跌"); 
                                if(parseInt(result2.data.close,10) < parseFloat(result2.data.ssprice)){
                                    //在黄线下方
                                    // if((parseInt(result2.data.close,10)) < parseFloat(result2.data.sprice)){
                                        //第一手买跌
                                        toastLog("双针探底,第一轮,买跌;价格:"+result2.data.close+";数量:"+arr[0]); 

                                        sellDTask(parseInt(result2.data.close,10),parseInt(arr[0],10));
                                        runup = 2; 
                                        runstutas = 1; 
                                        runvalue = parseInt(result2.data.close,10); 
                                    // }else{
                                    //     toastLog("双针探底,买跌,k值在60线上方"); 
                                    // }
                                }else{
                                    toastLog("双针探底,买跌,k值在60线上方"); 
                                }
                            }
                        }else{
                            toast("还有挂单，无法开启脚本");
                        }

                    }else if(runstutas == 1){
                        //没买入成功，撤销回归初始位置
                        if(parseInt(numt,10) == 0){
                            toastLog("双针探底,第一轮,没买入成功，撤销回归初始位置");  
                            if(runup == 1){
                                //撤销买跌订单
                                cancelBuyTask();
                                runup = 0; 
                                runstutas = 0; 
                                runvalue = 0; 
                            }

                            if(runup == 2){
                                //撤销买涨订单
                                cancelSellTask();
                                runup = 0; 
                                runstutas = 0; 
                                runvalue = 0; 
                            }
                        }else{
                           //买入成功，加2个点挂卖出
                            toastLog("双针探底,第一轮,买入成功，加2个点挂卖出");  
                            if(runup == 1){
                                sellZTask((runvalue+2),parseInt(numt,10));
                                runstutas = 2; 
                            }
                            //买入成功，减2个点挂卖出
                            if(runup == 2){
                                buyZTask((runvalue-2),parseInt(numt,10));
                                runstutas = 2; 
                            }
                        }
                    }else if(runstutas == 2){
                        //第一手 卖出成功
                        if(parseInt(numt,10) == 0){
                            toastLog("双针探底,第一轮,卖出成功");  
                            runup = 0; 
                            runstutas = 0; 
                            runvalue = 0; 
                        }else{
                            // 到达点位，第一手未卖出，到达点位，撤销订单，挂第二手 买入
                            toastLog("双针探底,到达点位，第一手未卖出，到达点位，撤销订单，挂第二手 买入");  
                            if(runup == 1){
                                if(runvalue - parseInt(result2.data.close,10) > 1){
                                    //撤销未卖出的买多订单
                                    cancelSellTask();
                                    sleep(300);
                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runstutas = 3; 
                                    runvalue = parseInt(result2.data.close,10); 
                                }
                            }
                            //买跌 涨两个点位继续买入
                            if(runup == 2){
                                if(parseInt(result2.data.close,10) - runvalue > 1){
                                    //撤销未卖出的买跌订单
                                    cancelBuyTask();
                                    sleep(1000);
                                    sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runstutas = 3; 
                                    runvalue = parseInt(result2.data.close,10); 
                                }
                            }
                        } 
                    }else if(runstutas == 3){
                        //买入成功,加两个点卖出
                        if(parseInt(numt,10) >= (parseInt(arr[0],10)+parseInt(arr[1],10))){
                            toastLog("双针探底,第二手买入成功，加减一个点卖出");  
                            //加一个点挂卖出
                            if(runup == 1){
                                sellZTask((runvalue+1),parseInt(numt,10));
                                runstutas = 4; //挂卖出
                            }
                            //买跌 涨两个点位继续买入
                            if(runup == 2){
                                buyZTask((runvalue-1),parseInt(numt,10));
                                runstutas = 4; //挂卖出
                            }
                        }else{
                             //未买入成功
                             toastLog("双针探底，第二手买入失败，根据情况判断按照当前点位买入，还是返回上一轮");
                             if(runup == 1){
                                 //假设当前金额大于上一轮的金额，按照当前金额买入
                                 if(parseInt(result2.data.close,10)>runvalue){
                                     cancelBuyTask();
                                     sleep(300);
                                     buyDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                     runvalue = parseInt(result2.data.close,10);
                                 }
                                 //小于上一轮的价格
                                 if(parseInt(result2.data.close,10)<runvalue){
                                     cancelBuyTask();
                                     sleep(300);
                                     sellZTask((runvalue+4),parseInt(numt,10));
                                     runstutas = 2;
                                     runvalue = runvalue+2;
                                 }
 
                             }
                             //买跌 涨两个点位继续买入
                             if(runup == 2){
                                 if(parseInt(result2.data.close,10)<runvalue){
                                     cancelSellTask();
                                     sleep(300);
                                     sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                     runvalue = parseInt(result2.data.close,10);
                                 }
 
                                  //小于上一轮的价格
                                  if(parseInt(result2.data.close,10)>runvalue){
                                     cancelSellTask();
                                     sleep(300);
                                     buyZTask((runvalue-4),parseInt(numt,10));
                                     runstutas = 2;
                                     runvalue = runvalue-2;
                                 }
                                
                             }
                        }
                    }else if(runstutas == 4){
                        //第二单卖出
                        if(parseInt(numt,10) == 0){
                            toastLog("双针探底，第二手卖出成功");
                            runup = 0; 
                            runstutas = 0;
                            runvalue = 0;
                        }else{
                            toastLog("双针探底，到达点位，第二手未卖出，撤销订单，挂第三手买入");
                            // 到达点位，第二手未卖出，撤销订单，挂第三手买入
                            if(runup == 1){
                                if(runvalue - parseInt(result2.data.close,10) > 1){
                                    //撤销卖出的订单
                                    cancelSellTask();
                                    sleep(300);
                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[2],10));
                                    runstutas = 5;
                                    runvalue = parseInt(result2.data.close,10);
                                }
                            }
                            //买跌 涨两个点位继续买入
                            if(runup == 2){
                                if(parseInt(result2.data.close,10) - runvalue > 1){
                                    cancelBuyTask();
                                    sleep(1000);
                                    sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runstutas = 5; 
                                    runvalue = parseInt(result2.data.close,10);
                                }
                            }
                        } 
                    }else if(runstutas == 5){
                        //买入成功
                        if(parseInt(numt,10) >= (parseInt(arr[0],10)+parseInt(arr[1],10)+parseInt(arr[2],10))){
                            toastLog("双针探底，第三手买入成功，挂卖出");
                            //加一个点挂卖出
                            if(runup == 1){
                                sellZTask((runvalue+1),parseInt(numt,10));
                                runstutas = 6; //挂卖出
                            }
                            //买跌 减一个点挂卖出
                            if(runup == 2){
                                buyZTask((runvalue-1),parseInt(numt,10));
                                runstutas = 6; //挂卖出
                            }
                        }else{
                            toastLog("双针探底，第三手未买入成功，重新买入或回上一轮");
                            //未买入成功，重新买入
                            if(runup == 1){
                                toastLog("第三手买入失败");
                                if(parseInt(result2.data.close,10)<runvalue){
                                    cancelBuyTask();
                                    sleep(300);
                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[2],10));
                                    runvalue = parseInt(result2.data.close,10); 
                                }
                                //小于上一轮的价格
                                if(parseInt(result2.data.close,10)<runvalue){
                                    cancelBuyTask();
                                    sleep(300);
                                    sellZTask((runvalue+4),parseInt(numt,10));
                                    runstutas = 4;
                                    runvalue = runvalue+2;
                                }
                            }
                            //买跌 涨两个点位继续买入
                            if(runup == 2){
                                if(parseInt(result2.data.close,10)<runvalue){
                                    cancelSellTask();
                                    sleep(300);
                                    sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runvalue = parseInt(result2.data.close,10);
                                }
                                //小于上一轮的价格
                                if(parseInt(result2.data.close,10)>runvalue){
                                    cancelSellTask();
                                    sleep(300);
                                    buyZTask((runvalue-4),parseInt(numt,10));
                                    runstutas = 4;
                                    runvalue = runvalue-2;
                                }
                            }
                        }

                    }else if(runstutas == 6){
                        if(parseInt(numt,10) == 0){
                            toastLog("双针探底，第三单卖出成功");
                            runup = 0;
                            runstutas = 0; 
                            runvalue = 0; 
                        }else{
                            if(runup == 1){
                                if(runvalue - parseInt(result2.data.close,10) > 0){
                                    toastLog("双针探底，第三手未卖出，跌一个点止损");
                                    cancelSellTask();
                                    sleep(300);
                                    sellZTask(parseInt(result2.data.close,10),parseInt(numt,10));
                                }
                            }
                            if(runup == 2){
                                if(parseInt(result2.data.close,10) - runvalue > 0){
                                    toastLog("双针探底，第三手未卖出，涨一个点止损");
                                    cancelBuyTask();
                                    sleep(300);
                                    buyZTask(parseInt(result2.data.close,10)-1,parseInt(numt,10));
                                }
                            }
                        } 
                    }
                }else{
                    toast("网络不通");
                }
            }else{
                toast("网络不通");
            }
        }  
        // 休眠一秒，避免过于频繁的循环  
        sleep(2000); 
    }
}

let runup1 = 0
let runstutas1 = 0  
let runvalue1 = 0
function run1() {
    let in1 = files.read(rootPath+"in2.txt");
    toastLog("多空指针,开始.."+in1);
    let arr = in1.split(',');
    while (true) {
        let currentSecond = getCurrentSecond(); 
        if (currentSecond > 5 && currentSecond < 8) {
            toastLog("多空指针,程序运行中.."+currentSecond);
        }  

        if (currentSecond > 15 && currentSecond < 18) {
            toastLog("多空指针,程序运行中.."+currentSecond);
        } 

        if (currentSecond > 25 && currentSecond < 28) {
            toastLog("多空指针,程序运行中.."+currentSecond);
        }

        if (currentSecond > 35 && currentSecond < 38) {
            toastLog("多空指针,程序运行中.."+currentSecond);
        } 

        if (currentSecond > 45 && currentSecond < 48) {
            toastLog("多空指针,程序运行中.."+currentSecond);
        }
        if (currentSecond > 57) {  
            let url2 = "https://anauto.millsecond.cn/api/agro/get_opportunity";
            r = http.get(url2, {
                headers: {
                    'Accept-Language': 'zh-cn,zh;q=0.5',
                    'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
                }
            });
            let result2 = r.body.json();
            if(result2.code == 200){
                const postData = {  mobile: content,buildId:buildId() };
                let url = "https://anauto.millsecond.cn/api/user/orderList";
                r = http.postJson(url, postData);
                let result = r.body.json();
                if(result.code == 200){
                    let numt = result.data.num;
                    if(runstutas1 == 0){
                            if(parseInt(numt,10) == 0){
                                // if(parseInt(result2.data.close,10) >= parseInt(result2.data.zc,10)){
                                //     if(parseInt(result2.data.close,10) - parseInt(result2.data.zc,10) < 3){
                                //         toastLog("双针探底,zc线太近");  
                                //         continue;       
                                //     }
                                // }
                                // if(parseInt(result2.data.close,10) <= parseInt(result2.data.yl,10)){
                                //     if(parseInt(result2.data.yl,10) - parseInt(result2.data.close,10)  < 3){
                                //         toastLog("双针探底,yl线太近"); 
                                //         continue;       
                                //     }
                                // }
                                // if(parseInt(result2.data.oppo,10) ==1){
                                //     // //买跌
                                //     if(parseInt(result2.data.tide,10) == 0){
                                //         //中线整体趋势向下
                                //         if(parseInt(result2.data.close,10) < parseFloat(result2.data.ssprice)){
                                //             //多空线在中线下方
                                //             if(parseFloat(result2.data.sprice) < parseFloat(result2.data.ssprice)){
                                //                 //K线实体底部在黄线下方
                                //                 if((parseInt(result2.data.close,10)+1) < parseFloat(result2.data.sprice)){
                                //                     sellDTask(parseInt(result2.data.close,10),parseInt(arr[0],10));
                                //                     runup1 = 2; 
                                //                     runstutas1 = 1; 
                                //                     runvalue1 = parseInt(result2.data.close,10);
                                //                     toastLog("多空指针,第一轮,买跌;价格:"+result2.data.close+";数量:"+arr[0]);  
                                //                 }else{
                                //                     toastLog("多空指针,买跌,K线实体底部在黄线上方");  
                                //                 }

                                //             }else{
                                //                 toastLog("多空指针,买跌,多空线在中线上方");
                                //             }
                                //         }else{
                                //             toastLog("多空指针,买跌,K值在在中线上方");
                                //         }
                                //     }

                                //     //买多
                                //     if(parseInt(result2.data.tide,10) == 1){
                                //         //中线整体趋势向上
                                //         if(parseInt(result2.data.close,10) > parseFloat(result2.data.ssprice)){
                                //             //多空线在中线上方
                                //             if(parseFloat(result2.data.sprice) > parseFloat(result2.data.ssprice)){
                                //                 //K线实体底部在黄线上方
                                //                 if((parseInt(result2.data.close,10)-1) > parseFloat(result2.data.sprice)){
                                                    //K线实体底部在黄线上
                                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[0],10));
                                                    runup1 = 1; 
                                                    runstutas1 = 1; 
                                                    runvalue1 = parseInt(result2.data.close,10); 
                                                    toastLog("多空指针,第一轮:买涨;价格-"+result2.data.close+";数量-"+arr[0]); 
                                //                 }else{
                                //                     toastLog("多空指针,买涨,K线实体底部在黄线下方");  
                                //                 }
                                //             }else{
                                //                 toastLog("多空指针,买涨,多空线在中线下方");
                                //             }
                                //         }else{
                                //             toastLog("多空指针,买涨,K值在在中线下方");
                                //         }
                                //     }
                                // }
                            }else{
                                toast("还有挂单，无法开启脚本");
                            }
                    }else if(runstutas1 == 1){
                        //没买入成功，撤销回归初始位置
                        if(parseInt(numt,10) == 0){
                            toastLog("多空指针，第一手买入失败，订单撤回");
                            if(runup1 == 1){
                                //撤销买跌订单
                                cancelBuyTask();
                                runup1 = 0; 
                                runstutas1 = 0; 
                                runvalue1 = 0; 
                            }

                            if(runup1 == 2){
                                //撤销买涨订单
                                cancelSellTask();
                                runup1 = 0; 
                                runstutas1 = 0; 
                                runvalue1 = 0; 
                            }
                        }else{
                            toastLog("多空指针，第一手买入成功，订单加两减个点卖出");
                           //买入成功，加2个点挂卖出
                            if(runup1 == 1){
                                sellZTask((runvalue1+2),parseInt(numt,10));
                                runstutas1 = 2; 
                            }
                            //买入成功，减2个点挂卖出
                            if(runup1 == 2){
                                buyZTask((runvalue1-2),parseInt(numt,10));
                                runstutas1 = 2; 
                            }
                        }
                    }else if(runstutas1 == 2){
                        //第一手 卖出成功
                        if(parseInt(numt,10) == 0){
                            toastLog("多空指针，第一手卖出成功");
                            runup1 = 0; 
                            runstutas1 = 0; 
                            runvalue1 = 0;
                        }else{
                            // 到达点位，第一手未卖出，撤销订单，挂第二手 买入
                            if(runup1 == 1){
                                if(runvalue1 - parseInt(result2.data.close,10) > 1){
                                    toastLog("多空指针，到达点位，第一手未卖出，撤销订单，挂第二手 买入");
                                    cancelSellTask();
                                    sleep(300);
                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runstutas1 = 3;
                                    runvalue1 = parseInt(result2.data.close,10);
                                }
                            }
                            //买跌 涨两个点位继续买入
                            if(runup1 == 2){
                                if(parseInt(result2.data.close,10) - runvalue1 > 1){
                                    toastLog("多空指针，到达点位，第一手未卖出，撤销订单，挂第二手 买入");
                                    cancelBuyTask();
                                    sleep(300);
                                    sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runstutas1 = 3;
                                    runvalue1 = parseInt(result2.data.close,10);
                                }
                            }
                        } 
                    }else if(runstutas1 == 3){
                        if(parseInt(numt,10) >= (parseInt(arr[0],10)+parseInt(arr[1],10))){
                            toastLog("多空指针，第二手买入成功 ，加减一个点卖出");
                            if(runup1 == 1){
                                sellZTask((runvalue1+1),parseInt(numt,10));
                                runstutas1 = 4;
                            }
                            //买跌 涨两个点位继续买入
                            if(runup1 == 2){
                                buyZTask((runvalue1-1),parseInt(numt,10));
                                runstutas1 = 4;
                            }
                        }else{
                            //未买入成功
                            toastLog("多空指针，第二手买入失败，重新按照当前点位买入");
                            if(runup1 == 1){
                                //假设当前金额大于上一轮的金额，按照当前金额买入
                                if(parseInt(result2.data.close,10)>runvalue1){
                                    cancelBuyTask();
                                    sleep(300);
                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runvalue1 = parseInt(result2.data.close,10);
                                }
                                //小于上一轮的价格
                                if(parseInt(result2.data.close,10)<runvalue1){
                                    cancelBuyTask();
                                    sleep(300);
                                    sellZTask((runvalue1+4),parseInt(numt,10));
                                    runstutas1 = 2;
                                    runvalue1 = runvalue1+2;
                                }

                            }
                            //买跌 涨两个点位继续买入
                            if(runup1 == 2){
                                if(parseInt(result2.data.close,10)<runvalue1){
                                    cancelSellTask();
                                    sleep(300);
                                    sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runvalue1 = parseInt(result2.data.close,10);
                                }

                                 //小于上一轮的价格
                                 if(parseInt(result2.data.close,10)>runvalue1){
                                    cancelSellTask();
                                    sleep(300);
                                    buyZTask((runvalue1-4),parseInt(numt,10));
                                    runstutas1 = 2;
                                    runvalue1 = runvalue1-2;
                                }
                               
                            }
                        }
                    }else if(runstutas1 == 4){
                        //第二单卖出
                        if(parseInt(numt,10) == 0){
                            toastLog("多空指针，第二手卖成功");
                            runup1 = 0;
                            runstutas1 = 0;
                            runvalue1 = 0;
                        }else{
                            // 到达点位，第二手未卖出，撤销订单，挂第三手买入
                            if(runup1 == 1){
                                if(runvalue1 - parseInt(result2.data.close,10) > 1){
                                    toastLog("到达点位，第二手未卖出，撤销订单，挂第三手 买入");
                                    cancelSellTask();
                                    sleep(300);
                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[2],10));
                                    runstutas1 = 5; 
                                    runvalue1 = parseInt(result2.data.close,10);
                                }
                            }
                            //买跌 涨两个点位继续买入
                            if(runup1 == 2){
                                if(parseInt(result2.data.close,10) - runvalue1 > 1){
                                    toastLog("到达点位，第二手未卖出，撤销订单，挂第三手 买入");
                                    cancelBuyTask();
                                    sleep(300);
                                    sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runstutas1 = 5;
                                    runvalue1 = parseInt(result2.data.close,10);
                                }
                            }
                        } 
                    }else if(runstutas1 == 5){
                        //判断第三手买入
                        if(parseInt(numt,10) >= (parseInt(arr[0],10)+parseInt(arr[1],10)+parseInt(arr[2],10))){
                            //加一个点挂卖出
                            toastLog("到达点位，第三手买入成功，加两减个点卖出");
                            if(runup1 == 1){
                                sellZTask((runvalue1+1),parseInt(numt,10));
                                runstutas1 = 6; 
                            }
                            //买跌 减一个点挂卖出
                            if(runup1 == 2){
                                buyZTask((runvalue1-1),parseInt(numt,10));
                                runstutas1 = 6; 
                            }
                        }else{
                            //未买入成功，重新买入
                            if(runup1 == 1){
                                toastLog("第三手买入失败");
                                if(parseInt(result2.data.close,10)<runvalue1){
                                    cancelBuyTask();
                                    sleep(300);
                                    //操作买多
                                    buyDTask(parseInt(result2.data.close,10),parseInt(arr[2],10));
                                    runvalue1 = parseInt(result2.data.close,10); //当前价格
                                }
                                //小于上一轮的价格
                                if(parseInt(result2.data.close,10)<runvalue1){
                                    cancelBuyTask();
                                    sleep(300);
                                    sellZTask((runvalue1+4),parseInt(numt,10));
                                    runstutas1 = 4;
                                    runvalue1 = runvalue1+2;
                                }
                            }
                            //买跌 涨两个点位继续买入
                            if(runup1 == 2){
                                if(parseInt(result2.data.close,10)<runvalue1){
                                    cancelSellTask();
                                    sleep(300);
                                    sellDTask(parseInt(result2.data.close,10),parseInt(arr[1],10));
                                    runvalue1 = parseInt(result2.data.close,10);
                                }
                                //小于上一轮的价格
                                if(parseInt(result2.data.close,10)>runvalue1){
                                    cancelSellTask();
                                    sleep(300);
                                    buyZTask((runvalue1-4),parseInt(numt,10));
                                    runstutas1 = 4;
                                    runvalue1 = runvalue1-2;
                                }
                            }
                        }
                    }else if(runstutas1 == 6){
                        //第三单卖出
                        if(parseInt(numt,10) == 0){
                            toastLog("第三手卖出成功");
                            runup1 = 0;
                            runstutas1 = 0; 
                            runvalue1 = 0; 
                        }else{
                            // 到达点位，第三手未卖出，涨一个点止损
                            if(runup1 == 1){
                                if(runvalue1 - parseInt(result2.data.close,10) > 0){
                                    toastLog("到达点位，第三手未卖出，涨一个点止损");
                                    cancelSellTask();
                                    sleep(300);
                                    sellZTask(parseInt(result2.data.close,10),parseInt(numt,10));
                                }
                            }
                            //买跌 涨两个点位继续买入
                            if(runup1 == 2){
                                if(parseInt(result2.data.close,10) - runvalue1 > 0){
                                    toastLog("到达点位，第三手未卖出，涨一个点止损");
                                    cancelBuyTask();
                                    sleep(300);
                                    buyZTask(parseInt(result2.data.close,10)-1,parseInt(numt,10));
                                }
                            }
                        } 
                    }
                }else{
                    toast("网络不通");
                }
            }else{
                toast("网络不通");
            }

        }  
        // 休眠一秒，避免过于频繁的循环  
        sleep(2000);  
    }
}

let runup2 = 0
let runstutas2 = 0  
let runvalue2 = 0
let upvlue2 = 0 //上一轮的价格

function run2() {
    let in2 = files.read(rootPath+"in3.txt");
    toastLog("趋势顺打,开始..."+in2);
    while (true) {
        let currentSecond = getCurrentSecond();

        if (currentSecond > 5 && currentSecond < 8) {
            toastLog("趋势顺打,程序运行中.."+currentSecond);
        }  

        if (currentSecond > 15 && currentSecond < 18) {
            toastLog("趋势顺打,程序运行中.."+currentSecond);
        } 

        if (currentSecond > 25 && currentSecond < 28) {
            toastLog("趋势顺打,程序运行中.."+currentSecond);
        }

        if (currentSecond > 35 && currentSecond < 38) {
            toastLog("趋势顺打,程序运行中.."+currentSecond);
        } 

        if (currentSecond > 45 && currentSecond < 48) {
            toastLog("趋势顺打,程序运行中.."+currentSecond);
        }

        if (currentSecond > 57) {   
            let url2 = "https://anauto.millsecond.cn/api/agro/get_opportunity";
            r = http.get(url2, {
                headers: {
                    'Accept-Language': 'zh-cn,zh;q=0.5',
                    'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
                }
            });
            let result2 = r.body.json();
            if(result2.code == 200){
                const postData = {  mobile: content,buildId:buildId() };
                let url = "https://anauto.millsecond.cn/api/user/orderList";
                r = http.postJson(url, postData);
                let result = r.body.json();
                if(result.code == 200){
                    let numt = result.data.num;
                    if(runstutas2 == 0){
                        if(parseInt(result2.data.close,10) >= parseInt(result2.data.zc,10)){
                            if(parseInt(result2.data.close,10) - parseInt(result2.data.zc,10) < 3){
                                toastLog("趋势顺打,距离支撑线3个点");
                                continue;       
                            }
                        }
                        if(parseInt(result2.data.close,10) <= parseInt(result2.data.yl,10)){
                            if(parseInt(result2.data.yl,10) - parseInt(result2.data.close,10)  < 3){
                                toastLog("趋势顺打,距离压力线3个点");
                                continue;       
                            }
                        }
                        //在60线上方  中线整体趋势向上
                        //判断阴线阳线 阴线
                        if(parseInt(result2.data.close,10) < upvlue2){
                            toastLog("阴线");
                            if(parseInt(result2.data.close,10) > parseFloat(result2.data.ssprice)){
                                //黄线在60线上方 多空线在中线上方
                                if(parseFloat(result2.data.price) > parseFloat(result2.data.ssprice)){
                                    //阴线下影线顶到多空线
                                    if((parseInt(result2.data.close,10)-1) < parseFloat(result2.data.price)){
                                        //K线实体在黄线上方
                                        if(parseInt(result2.data.close,10) > parseFloat(result2.data.sprice)){
                                            buyDTask(parseInt(result2.data.close,10),parseFloat(in2));
                                            runup2 = 1; 
                                            runstutas2 = 1; 
                                            runvalue2 = parseInt(result2.data.close,10); 
                                            toastLog("趋势顺打,买涨");
                                            continue;  
                                        }else{
                                            toastLog("做多,K线实体不在黄线上方");
                                        }
                                    }else{
                                        toastLog("做多,阴线下影线没顶到多空线");
                                    }
                                }else{
                                    toastLog("做多,多空线不在中线上方");
                                }
                            }
                        }
                        
                        //阳线
                        if(parseInt(result2.data.close,10) > upvlue2){
                            toastLog("阳线");
                            //在60线上方  中线整体趋势向下
                            if(parseInt(result2.data.close,10) < parseFloat(result2.data.ssprice)){
                                //黄线在60线上方 多空线在中线下方
                                if(parseFloat(result2.data.price) < parseFloat(result2.data.ssprice)){
                                    //阳线下影线顶到多空线
                                    if((parseInt(result2.data.close,10)+1) > parseFloat(result2.data.price)){
                                        //K线实体在黄线上方
                                        if(parseInt(result2.data.close,10) < parseFloat(result2.data.sprice)){
                                            sellDTask(parseInt(result2.data.close,10),parseFloat(in2));
                                            runup2 = 2; //买多
                                            runstutas2 = 1; //挂买入
                                            runvalue2 = parseInt(result2.data.close,10); 
                                            toastLog("趋势顺打,买跌");
                                        }else{
                                            toastLog("做空,K线实体不在黄线下方");
                                        }
                                    }else{ 
                                        toastLog("做空,阳线上影线没顶到多空线");
                                    }
                                }else{
                                    toastLog("做空,多空线不在中线下方");
                                }
                            }else{
                                toastLog("阳线,中线整体趋势向上");
                            }
                        }

                    }else if(runstutas2 == 1){
                        if(parseInt(numt,10) == 0){
                            if(runup2 == 1){
                                //撤销买跌订单
                                cancelBuyTask();
                                runup2 = 0;
                                runstutas2 = 0; 
                                runvalue2 = 0;
                            }

                            if(runup2 == 2){
                                //撤销买涨订单
                                cancelSellTask();
                                runup2 = 0;
                                runstutas2 = 0;
                                runvalue2 = 0; 
                            }
                        }else{
                            //买入成功，加2个点挂卖出
                            if(runup2 == 1){
                                if(parseInt(result2.data.close,10) - runvalue2 >1){
                                    sellZTask(parseInt(result2.data.close,10),parseInt(numt,10));
                                    runstutas2 = 2; 
                                }
                                if(runvalue2 - parseInt(result2.data.close,10) >1){
                                    sellZTask(parseInt(result2.data.close,10),parseInt(numt,10));
                                    runstutas2 = 2; 
                                }
                            }
                            if(runup2 == 2){
                                if(parseInt(result2.data.close,10) - runvalue2 >1){
                                    buyZTask(parseInt(result2.data.close,10),parseInt(numt,10));
                                    runstutas2 = 2; 
                                }
                                if(runvalue2 - parseInt(result2.data.close,10) >1){
                                    buyZTask(parseInt(result2.data.close,10),parseInt(numt,10));
                                    runstutas2 = 2; 
                                }
                            }
                        }

                    }else if(runstutas2 == 2){
                        if(parseInt(numt,10) == 0){
                            runup2 = 0; 
                            runstutas2 = 0; 
                            runvalue2 = 0;
                        }
                    }

                }
                upvlue2 = parseInt(result2.data.close,10);
            }
        } 
        sleep(2000); 
    }
}

module.exports = [run,run1,run2];